package com.yanqu.road.server.manger.cricket.skillhandler.attround.handler;

import com.yanqu.road.entity.cricket.helper.AddHpResult;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.attround.CricketSkillAttRoundStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill166Strategy implements CricketSkillAttRoundStrategy {

    /**
     * 技能处理-每当收到超过生命{0}%的伤害时，减少{2}技能id冷却{1}回合
     * @param cricketGame 本局对局对象
     * @param skillData 触发的技能
     * @param baseAttSkill 基础攻击技能
     * @param baseAttReHpResult 基础攻击结果对象
     * @param action 我方
     * @param target 对方
     * @param attackRoundBuilder 返回PB结构体
     * @return true：死亡，退出技能处理，false：继续技能处理
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, SkillData baseAttSkill, AddHpResult baseAttReHpResult, BattleData action, BattleData target, CricketProto.CricketAttackRound.Builder attackRoundBuilder, long battleFrameId) {
        //基础攻击miss，不触发技能
        if (baseAttReHpResult == null) {
            return false;
        }
        int attackTimes = baseAttSkill.getSkillEffect().getP2();
        long attack = Math.abs(baseAttReHpResult.getValue());
        long perAttack = BigDecimal.valueOf(attack).divide(BigDecimal.valueOf(attackTimes), 8, RoundingMode.DOWN).longValue();
        //每当收到超过生命{0}%的普通攻击伤害时，减少{2}技能id冷却{1}回合
        int decHpRatio = BigDecimal.valueOf(perAttack).multiply(BigDecimal.valueOf(1000)).divide(BigDecimal.valueOf(target.getMaxHp()), 8, RoundingMode.DOWN).intValue();
        if (decHpRatio >= skillData.getSkillEffect().getP1()) {
            int checkSkillId = skillData.getSkillEffect().getP3();
            target.decSkillCoolingDown(checkSkillId, skillData.getSkillEffect().getP2() * attackTimes);
            CricketProto.CricketBattleFrame.Builder skill166FrameBuilder = cricketGame.newBattleFrameBuilder();
            skill166FrameBuilder.setActionUserId(target.getUserId());
            skill166FrameBuilder.setTargetUserId(target.getUserId());
            skill166FrameBuilder.setSkillId(skillData.getSkillId());
            skill166FrameBuilder.setAttackTimes(attackTimes);
            cricketGame.fillRemainHp(skill166FrameBuilder);
            attackRoundBuilder.addAttack(skill166FrameBuilder);
        }
        return false;
    }
}
